$(function () {

    var client_pool_datagrid = $("#client_pool_datagrid");
    var client_pool_check_dialog = $("#client_pool_check_dialog");
    var client_pool_check_form = $("#client_pool_check_form");
    var appoint_client_dialog = $("#appoint_client_dialog");
    var appoint_client_form = $("#appoint_client_form");

    var cmdObj = {
        // 查看客户池中的客户
        detailsClientInPool: function () {
            var currentRow = client_pool_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请先选中一行数据', 'error');
                return;
            }

            client_pool_check_form.form('clear');

            if (currentRow.gender) {
                currentRow['gender.id'] = currentRow.gender.id;
            }

            if (currentRow.wishLevel) {
                currentRow['wishLevel.id'] = currentRow.wishLevel.id;
            }

            if (currentRow.wishCampus) {
                currentRow['wishCampus.id'] = currentRow.wishCampus.id;
            }

            if (currentRow.wishClass) {
                currentRow['wishClass.id'] = currentRow.wishClass.id;
            }

            if (currentRow.wishSubject) {
                currentRow['wishSubject.id'] = currentRow.wishSubject.id;
            }

            if (currentRow.clientType) {
                currentRow['clientType.id'] = currentRow.clientType.id;
            }

            if (currentRow.bigCustomer) {
                currentRow['bigCustomer.id'] = currentRow.bigCustomer.id;
            }

            if (currentRow.foreignLangLevel) {
                currentRow['foreignLangLevel.id'] = currentRow.foreignLangLevel.id;
            }

            if (currentRow.inputEmployee) {
                currentRow['inputEmployee.id'] = currentRow.inputEmployee.id;
            }

            client_pool_check_form.form('load', currentRow);

            // 将表单设置为只读
            $(".easyui-combobox").combobox('readonly', true);
            $(".easyui-datebox").datebox('readonly', true);
            $(".easyui-textbox").textbox('readonly', true);

            client_pool_check_dialog.dialog('setTitle', '查看详情');
            client_pool_check_dialog.dialog('open');
        },
        reloadPool: function () {
            client_pool_datagrid.datagrid('reload');
        },
        // 接手潜在客户
        requestClient: function () {
            var currentRow = client_pool_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请问要将哪位客户交给你?', 'info');
                return;
            }
            $.messager.confirm('提示', '你确定要接手该客户吗?', function (ret) {
                if (ret) {
                    $.post('/clientPool/receive', {clientId: currentRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '接手成功,加油!', 'info');
                            cmdObj.iknow();
                            cmdObj.reloadPool();
                        } else {
                            $.messager.alert('提示', data.msg, 'error');
                        }
                    });
                }
            });
        },
        // 指派客户
        appointClient: function () {
            var currentRow = client_pool_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择被指派的客户', 'info');
                return;
            }
            appoint_client_form.form('clear');
            appoint_client_form.form('load', currentRow);
            appoint_client_dialog.dialog('open');
        },
        // 查看确认
        iknow: function () {
            client_pool_check_dialog.dialog('close');
        },
        // 指派确认
        appoint_submit: function () {
            /* appoint_client_form.form('submit', {
             url: '/clientPool/appoint',
             success: function (data) {

             }
             })*/
            $.messager.confirm('提示', '确定要指派任务吗?', function (ret) {
                if (ret) {
                    appoint_client_form.form('submit', {
                        url: '/clientPool/appoint',
                        success: function (data) {
                            data = $.parseJSON(data);
                            if (data.success) {
                                $.messager.alert('提示', '操作成功', 'info');
                                cmdObj.appoint_cancel();
                                client_pool_datagrid.datagrid('reload');
                            } else {
                                $.messager.alert('提示', data.msg, 'error');
                            }
                        }
                    })
                }
            });
        },
        // 指派取消
        appoint_cancel: function () {
            appoint_client_dialog.dialog('close');
        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    })

    client_pool_datagrid.datagrid({
        url: '/clientPool/query',
        fit: true,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        width: 1000,
        columns: [[
            {field: 'id', title: '编号', halign: 'center', align: 'center', width: 100, hidden: true},
            {field: 'name', title: '姓名', halign: 'center', align: 'center', width: 100},
            {
                field: 'inputEmployee', title: '录入人', halign: 'center', align: 'center', width: 100,
                formatter: formatInputEmployee
            },
            {field: 'buildDocTime', title: '建档时间', halign: 'center', align: 'center', width: 100},
            {field: 'traceNumber', title: '跟进次数', halign: 'center', align: 'center', width: 100},
            {field: 'lastTraceTime', title: '最后跟进时间', halign: 'center', align: 'center', width: 100},
            {field: 'tel', title: '电话', halign: 'center', align: 'center', width: 100},
            {field: 'email', title: '邮箱', halign: 'center', align: 'center', width: 100},
            {
                field: 'wishLevel', title: '意向等级', halign: 'center', align: 'center', width: 100,
                formatter: formatWishLevel
            },
            {
                field: 'wishSubject', title: '意向学科', halign: 'center', align: 'center', width: 100,
                formatter: formatWishSubject
            },
            {
                field: 'state', title: '状态', halign: 'center', align: 'center', width: 100,
                formatter: formatState
            },
            {field: 'remark', title: '备注', halign: 'center', align: 'center', width: 100}
        ]],
        toolbar: '#client_pool_datagrid_buttons'
    });

});

function formatInputEmployee(value, row, index) {
    return value == null ? '' : value.username;
}
function formatWishLevel(value, row, index) {
    return value == null ? '' : value.name;
}
function formatWishSubject(value, row, index) {
    return value == null ? '' : value.name;
}
function formatState(value, row, index) {
    return value == null ? '' : "<span style='color: red'>" + value.name + "</span>";
}



























